# Docker image for running CI/CD
FROM node:lts

ARG CYPRESS_VERSION=8.3.0

ENV PUPPETEER_DOWNLOAD_HOST=https://storage.googleapis.com.cnpmjs.org \
  PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
  PUPPETEER_EXECUTABLE_PATH=/usr/bin/google-chrome-stable \
  NODE_LTS_VERSION=14.x \
  NODE_MINIMAL_VERSION=v12.17.0 \
  DEBIAN_FRONTEND="noninteractive" \
  TZ="Asia/Shanghai" \
  RUSTUP_HOME=/usr/local/rustup \
  CARGO_HOME=/usr/local/cargo \
  RUSTUP_DIST_SERVER=https://rustup.byted.org \
  RUSTUP_UPDATE_ROOT=https://rustup.byted.org/rustup \
  PATH=/usr/local/cargo/bin:$PATH \
  # Chrome 64
  CHROMIUM_REVESION=538022 \
  http_proxy=http://10.20.47.147:3128 \
  https_proxy=http://10.20.47.147:3128 \
  NO_PROXY=*.byted.org,*.bytedance.net,mirrors.ustc.edu.cn,mirrors.aliyun.com,mirror.sjtu.edu.cn,cdn.*.taobao.org

RUN sed -i 's/http:\/\/.*debian.org/http:\/\/mirrors.byted.org/g' /etc/apt/sources.list && \
  apt-get update && \
  # common dev tools
  apt-get install curl wget unzip \
  libjpeg-dev zlib1g-dev software-properties-common \
  build-essential git vim openssh-client gpg-agent \
  xvfb libgtk2.0-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 \
  apt-transport-https default-jre -y --no-install-recommends --fix-missing && \
  # ansible source
  echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" >> /etc/apt/sources.list.d/ansible.list && \
  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367 && \
  # node source
  curl -sS http://mirrors.byted.org/nodesource/gpgkey/nodesource.gpg.key | apt-key add - && \
  echo "deb http://mirrors.byted.org/nodesource/deb_${NODE_LTS_VERSION}/ $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/nodesource.list && \
  # yarn source
  curl -sS http://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
  echo "deb http://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
  # chrome source
  curl -sS https://dl.google.com/linux/linux_signing_key.pub | apt-key add - && \
  echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list && \
  # docker source
  curl -sS http://mirrors.byted.org/docker/linux/debian/gpg | apt-key add - && \
  echo "deb [arch=amd64] http://mirrors.byted.org/docker/linux/debian `lsb_release -cs` stable" | tee /etc/apt/sources.list.d/docker.list && \
  # ansible & node & yarn & docker-ce & chrome
  apt-get update && \
  apt-get install ansible google-chrome-stable nodejs yarn docker-ce -y --no-install-recommends --fix-missing && \
  apt-get autoremove -y && apt-get purge && apt-get autoclean && \
  # yarn config
  yarn config set registry https://bnpm.byted.org && \
  yarn config set disturl https://bnpm.bytedance.net/mirrors/node && \
  yarn config set cache-folder /cache/.yarn && \
  npm config set registry https://bnpm.byted.org && \
  # old version node
  wget https://bnpm.bytedance.net/mirrors/node/${NODE_MINIMAL_VERSION}/node-${NODE_MINIMAL_VERSION}-linux-x64.tar.xz && \
  tar xf node-${NODE_MINIMAL_VERSION}-linux-x64.tar.xz && \
  rm node-${NODE_MINIMAL_VERSION}-linux-x64.tar.xz && \
  mv node-${NODE_MINIMAL_VERSION}-linux-x64 /node-legacy && \
  # rust
  curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
  # cypress
  wget https://bnpm.bytedance.net/mirrors/cypress/${CYPRESS_VERSION}/linux/cypress.zip && \
  unzip -qq cypress.zip && \
  mv Cypress /root && \
  chmod 777 /root/Cypress/Cypress && \
  rm cypress.zip && \
  # chrome-driver
  export CHROMEDRIVER_VERSION=`curl -sS https://bnpm.bytedance.net/mirrors/chromedriver/LATEST_RELEASE` && \
  mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
  wget https://bnpm.bytedance.net/mirrors/chromedriver/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
  unzip -qq chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
  rm chromedriver_linux64.zip && \
  chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
  ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver && \
  # old version chrome
  wget https://bnpm.bytedance.net/mirrors/chromium-browser-snapshots/Linux_x64/${CHROMIUM_REVESION}/chrome-linux.zip && \
  unzip chrome-linux.zip && \
  mv chrome-linux /root/chromium && \
  chmod 777 /root/chromium/chrome && \
  rm chrome-linux.zip && \
  echo "UTC" > /etc/timezone && \
  rm -rf /var/lib/apt/lists/*